<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="企业名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业法人" prop="legalPerson">
        <el-input
          v-model="queryParams.legalPerson"
          placeholder="请输入企业法人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="pic">
        <el-input
          v-model="queryParams.pic"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人电话" prop="picContact">
        <el-input
          v-model="queryParams.picContact"
          placeholder="请输入负责人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--   <el-form-item label="企业简称" prop="shortName">
        <el-input
          v-model="queryParams.shortName"
          placeholder="请输入企业简称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管辖区" prop="jurisdiction">
        <el-input
          v-model="queryParams.jurisdiction"
          placeholder="请输入管辖区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业编号-必填" prop="enterpriseNumber">
        <el-input
          v-model="queryParams.enterpriseNumber"
          placeholder="请输入企业编号-必填"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="最大车辆数" prop="maxCarNum">
        <el-input
          v-model="queryParams.maxCarNum"
          placeholder="请输入最大车辆数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!-- <el-form-item label="车管联系人" prop="vehicleManagementContact">
        <el-input
          v-model="queryParams.vehicleManagementContact"
          placeholder="请输入车管联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!-- <el-form-item label="车管联系人电话" prop="vehicleManagementContactPhone">
        <el-input
          v-model="queryParams.vehicleManagementContactPhone"
          placeholder="请输入车管联系人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="道路运输许可证号" prop="roadTransportPermitNumber">
        <el-input
          v-model="queryParams.roadTransportPermitNumber"
          placeholder="请输入道路运输许可证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建筑垃圾登记证号" prop="constructionWasteRegistrationNumber">
        <el-input
          v-model="queryParams.constructionWasteRegistrationNumber"
          placeholder="请输入建筑垃圾登记证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效期至" prop="validUntil">
        <el-date-picker clearable
          v-model="queryParams.validUntil"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期至">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="社会信用代码" prop="socialCreditCode">
        <el-input
          v-model="queryParams.socialCreditCode"
          placeholder="请输入社会信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="pic">
        <el-input
          v-model="queryParams.pic"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人电话" prop="picContact">
        <el-input
          v-model="queryParams.picContact"
          placeholder="请输入负责人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->

      <el-form-item label="所属省" prop="provinceCode">
        <el-select
          v-model="queryParams.provinceCode"
          clearable
          placeholder="请选择所属省"
          @change="provinceChange"
        >
          <el-option
            v-for="(v,index) in provinceList"
            :key="index"
            :label="v.provinceName"
            :value="v.provinceCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属市" prop="cityCode">
        <el-select
          v-model="queryParams.cityCode"
          clearable
          placeholder="请选择所属市"
          @change="cityChange"
        >
          <el-option
            v-for="(v,index) in cityList"
            :key="index"
            :label="v.cityName"
            :value="v.cityCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属县区" prop="areaCode">
        <el-select v-model="queryParams.areaCode" clearable placeholder="请选择区域">
          <el-option
            v-for="(v,index) in areaList"
            :key="index"
            :label="v.areaName"
            :value="v.areaCode"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8"> -->
    <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['modules:company:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['modules:company:edit']"
        >修改</el-button>
    </el-col>-->
    <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['modules:company:remove']"
        >删除</el-button>
    </el-col>-->
    <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['modules:company:export']"
        >导出</el-button>
    </el-col>-->
    <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table
      border
      fit
      highlight-current-row
      empty-text="暂无数据"
      v-loading="loading"
      :data="transportationCompanyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="idShow" />
      <el-table-column label="企业id" align="center" prop="oldTeamId" v-if="idShow" />
      <el-table-column label="关联账号userId" align="center" prop="userId" v-if="idShow" />

      <el-table-column label="企业名称" align="center" width="250px" prop="name" />
      <el-table-column label="企业法人" align="center" width="80px" prop="legalPerson" />
      <el-table-column label="负责人" align="center" width="120px" prop="pic" />
      <el-table-column label="负责人电话" align="center" width="120px" prop="picContact" />
      <el-table-column label="所属省" align="center" prop="provinceName" width="80" />
      <el-table-column label="所属市" align="center" prop="cityName" width="80" />
      <el-table-column label="所属县区" align="center" prop="areaName" width="80" />
      <!-- <el-table-column label="运输许可证号" align="center"  width="120px" prop="roadTransportPermitNumber" /> -->
      <el-table-column
        label="建筑垃圾登记证号"
        align="center"
        width="180px"
        prop="constructionWasteRegistrationNumber"
      />
      <el-table-column
        label="道路运输许可证号"
        align="center"
        width="150px"
        prop="roadTransportPermitNumber"
      />
      <!-- <el-table-column label="有效期至" align="center"  prop="validUntil" width="120px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validUntil, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->

      <el-table-column label="社会信用代码" align="center" width="150px" prop="socialCreditCode" />
      <el-table-column label="企业地址" align="center" width="250px" prop="address" />
      <el-table-column label="最大车辆数" align="center" width="120px" prop="maxCarNum">
        <template slot-scope="scope">
          <!-- <span>{{ getMaxCarNum(scope.row.maxCarNum) }}</span> -->
          <span>{{getMaxCarNum(scope.row.maxCarNum)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录账号是否创建" align="center" prop="userId" width="180" fixed="right">
        <template slot-scope="scope">
          <span
            span
            :class="scope.row.userId != null && scope.row.userId !== '' ? 'green' : 'red'"
          >{{ scope.row.userId != null && scope.row.userId != '' ? '已创建' : '未创建' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="180"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['modules:company:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDec(scope.row)"
            v-hasPermi="['modules:company:query']"
          >详情</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="bandUser(scope.row)"
            v-hasPermi="['modules:company:banduser']"
            v-if="scope.row.userId == null || scope.row.userId == ''"
          >绑定系统账号</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['modules:company:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运输公司信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="省" prop="provinceCode">
              <el-select
                v-model="form.provinceCode"
                clearable
                :disabled="isDisabled"
                placeholder="请选择省份"
                @change="formProvinceChange"
              >
                <el-option
                  v-for="(v,index) in provinceList"
                  :key="index"
                  :label="v.provinceName"
                  :value="v.provinceCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="市" prop="cityCode">
              <el-select
                v-model="form.cityCode"
                clearable
                :disabled="isDisabled"
                placeholder="请选择城市"
                @change="formCityChange"
              >
                <el-option
                  v-for="(v,index) in cityList"
                  :key="index"
                  :label="v.cityName"
                  :value="v.cityCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="县/区" prop="areaCode">
              <el-select
                v-model="form.areaCode"
                clearable
                :disabled="isDisabled"
                placeholder="请选择区域"
                @change="formAreaChange"
              >
                <el-option
                  v-for="(v,index) in areaList"
                  :key="index"
                  :label="v.areaName"
                  :value="v.areaCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="form.name" :disabled="isDisabled" placeholder="请输入企业名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="企业法人" prop="legalPerson">
              <el-input v-model="form.legalPerson" :disabled="isDisabled" placeholder="请输入企业法人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最大车辆数" prop="maxCarNum">
              <el-input v-model="form.maxCarNum" :disabled="isDisabled" placeholder="请输入最大车辆数" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="道路运输许可" prop="roadTransportPermitNumber">
              <el-input
                v-model="form.roadTransportPermitNumber"
                :disabled="isDisabled"
                placeholder="请输入道路运输许可证号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="建筑垃圾登记" prop="constructionWasteRegistrationNumber">
              <el-input
                v-model="form.constructionWasteRegistrationNumber"
                :disabled="isDisabled"
                placeholder="请输入建筑垃圾登记证号"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
        <el-form-item label="有效期至" prop="validUntil">
          <el-date-picker clearable
            v-model="form.validUntil"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效期至">
          </el-date-picker>
        </el-form-item>
          </el-col>-->
          <el-col :span="8">
            <el-form-item label="社会信用代码" prop="socialCreditCode">
              <el-input
                v-model="form.socialCreditCode"
                :disabled="isDisabled"
                placeholder="请输入社会信用代码"
              />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="企业地址" prop="address">
              <el-input v-model="form.address" :disabled="isDisabled" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人" prop="pic">
              <el-input v-model="form.pic" :disabled="isDisabled" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人电话" prop="picContact">
              <el-input v-model="form.picContact" :disabled="isDisabled" placeholder="请输入负责人电话" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="form.content"
                type="textarea"
                :disabled="isDisabled"
                :min-height="192"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary"  @click="submitForm">确 定</el-button> -->
        <el-button type="primary" @click="cancel">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 绑定系统账号 -->
    <tDetail v-if="showDetail" :visible="showDetail" :info="info" @closePop="closePop" @resetQuery="resetQuery"></tDetail>
  </div>
</template>

<script>
import {
  listTransportationCompany,
  getTransportationCompany,
  delTransportationCompany,
  addTransportationCompany,
  updateTransportationCompany
} from "@/api/modules/transportationCompany";
import sysConstant from "@/utils/sysConstant";
import { provinceList, cityList, areaList } from "@/api/region/region";
import tDetail from "./detail.vue";
export default {
  name: "TransportationCompany",
  components: {
    tDetail
  },
  data() {
    return {
      provinceList: [],
      cityList: [],
      areaList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运输公司信息表格数据
      transportationCompanyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      idShow: false,
      isDisabled: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oldTeamId: null,
        userId: null,
        name: null,
        shortName: null,
        enterpriseType: null,
        jurisdiction: null,
        enterpriseNumber: null,
        legalPerson: null,
        maxCarNum: null,
        vehicleManagementContact: null,
        vehicleManagementContactPhone: null,
        roadTransportPermitNumber: null,
        constructionWasteRegistrationNumber: null,
        validUntil: null,
        socialCreditCode: null,
        legalPersonIdAttachment: null,
        qualificationCertificateAttachment: null,
        businessLicenseAttachment: null,
        otherAttachment: null,
        exts: null,
        address: null,
        pic: null,
        picContact: null,
        content: null,
        provinceCode: null,
        provinceName: null,
        cityCode: null,
        cityName: null,
        areaCode: null,
        areaName: null,
        townName: null,
        townCode: null
      },
      showDetail: false, // 弹窗
      info: {}, // 弹窗信息
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.initProvinceList();
    this.initCityList(this.queryParams.provinceCode);
  },
  methods: {
    // 绑定系统账号
    bandUser(item) {
      this.info = item;
      this.showDetail = true;
    },
    // 关闭弹窗
    closePop() {
      this.showDetail = false;
    },
    initConstructionSiteList() {
      listSite().then(response => {
        this.constructionSiteList = response.data;
      });
    },
    initRegulationList() {
      listRegulation({ type: 0, status: 0 }).then(response => {
        this.regulationList = response.data;
      });
    },
    initProvinceList() {
      provinceList().then(response => {
        this.provinceList = response.data;
      });
    },
    initCityList(v) {
      let obj = {
        provinceCode: v
      };
      cityList(obj).then(response => {
        this.cityList = response.data;
      });
    },
    initAreaList(v) {
      let obj = {
        cityCode: v
      };
      areaList(obj).then(response => {
        this.areaList = response.data;
      });
    },
    //省份选择的
    provinceChange(v) {
      this.initCityList(v);
      this.queryParams.cityCode = null;
      this.queryParams.areaCode = null;
    },
    //省份选择的
    formProvinceChange(v) {
      this.initCityList(v);
      var obj = {};
      obj = this.provinceList.find(function(item) {
        return item.provinceCode === v;
      });
      console.log("province");
      console.log(obj);
      this.form.provinceName = obj.provinceName;
      this.cityList = [];
      this.areaList = [];
    },
    cityChange(v) {
      this.initAreaList(v);
      this.queryParams.areaCode = null;
    },
    //城市选择的
    formCityChange(v) {
      this.initAreaList(v);
      var obj = {};
      obj = this.cityList.find(function(item) {
        return item.cityCode === v;
      });
      console.log("city");
      console.log(obj);
      this.form.cityName = obj.cityName;
      this.form.cityCode = v;
      this.areaList = [];
    },
    formAreaChange(v) {
      var obj = {};
      obj = this.areaList.find(function(item) {
        return item.areaCode === v;
      });
      console.log("area");
      console.log(obj);
      this.form.areaName = obj.areaName;
      this.form.areaCode = v;
    },
    /** 查询运输公司信息列表 */
    getList() {
      this.loading = true;
      listTransportationCompany(this.queryParams).then(response => {
        this.transportationCompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        oldTeamId: null,
        userId: null,
        name: null,
        shortName: null,
        enterpriseType: null,
        jurisdiction: null,
        enterpriseNumber: null,
        legalPerson: null,
        maxCarNum: null,
        vehicleManagementContact: null,
        vehicleManagementContactPhone: null,
        roadTransportPermitNumber: null,
        constructionWasteRegistrationNumber: null,
        validUntil: null,
        socialCreditCode: null,
        legalPersonIdAttachment: null,
        qualificationCertificateAttachment: null,
        businessLicenseAttachment: null,
        otherAttachment: null,
        exts: null,
        address: null,
        pic: null,
        picContact: null,
        content: null,
        provinceCode: null,
        provinceName: null,
        cityCode: null,
        cityName: null,
        areaCode: null,
        areaName: null,
        townName: null,
        townCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.showDetail = false;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运输公司信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTransportationCompany(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运输公司信息";
      });
    },

    /** 详情按钮操作 */
    handleDec(row) {
      this.reset();
      const id = row.id || this.ids;
      getTransportationCompany(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.form.maxCarNum = this.getMaxCarNum(this.form.maxCarNum)
        
        this.isDisabled = true;
        this.title = "运输公司详情";
        // if(this.form.cityCode){
        //   this.this.initAreaList(this.form.cityCode);
        // }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTransportationCompany(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransportationCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除运输公司信息编号为"' + ids + '"的数据项？')
        .then(function() {
          return delTransportationCompany(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "modules/transportationCompany/export",
        {
          ...this.queryParams
        },
        `transportationCompany_${new Date().getTime()}.xlsx`
      );
    },
    
    // 获取最大车辆数
    getMaxCarNum(val) {
      let valObj = JSON.parse(val)
      let numVal = ''
      // if(val.includes('{')){
      if (typeof valObj === "string" || typeof valObj === "number") {
        numVal = valObj
      } else if (typeof valObj === "object") {
          if (Array.isArray(valObj)) {
            numVal = ''
          } else {
            numVal = valObj.num
          }
      }
      return numVal
    },
  }
};
</script>
<style scoped>
.green {
  color: green;
}

.red {
  color: red;
}
</style>